﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace KATS_Client.FileNameData
{
    /// <summary>Move Data</summary>
    public class MovieData
    {
        /// <summary>The name of the movie</summary>
        public string Name = string.Empty;
        /// <summary>The quality of the movie</summary>
        public string Quality = string.Empty;
        /// <summary>The year of the movie</summary>
        public int Year = 0;
        /// <summary>The file extension</summary>
        public string Extension = string.Empty;

        private MovieData(string name, string quality, int year, string extension)
        {
            this.Name = name;
            this.Quality = quality;
            this.Year = year;
            this.Extension = extension;
        }

        /// <summary>Get a formated filename</summary>
        public string FormatedFilename
        {
            get
            {
                string output = Name;
                if (Year >= 1930 && Year <= DateTime.Now.Year)
                {
                    output += string.Format(" [{0}]", Year);
                }
                if (Quality != string.Empty)
                {
                    output += string.Format(" [{0}]", Quality);
                }
                output = RemoveDoubleSpaces(output);
                output += Extension;
                return output;
            }
        }

        /// <summary>Parse a filename into movie data</summary>
        /// <param name="Filename"></param>
        /// <returns></returns>
        public static MovieData Parse(string Filename)
        {
            string name = string.Empty;
            string quality = string.Empty;
            int year = 0;

            string extension = GetExtension(Filename);
            string start = Filename.Replace(extension, string.Empty);


            // remove all bad and unnessesary characters
            char[] badChars = new char[] { '_', '.', '[', ']', '(', ')', '{', '}' };
            foreach (char c in badChars)
                start = start.Replace(c, ' ');
            // remove all double spaces
            start = RemoveDoubleSpaces(start);


            year = FindYear(start);
            quality = FindQuality(start);

            if (year == 0)
            {
                // year not found

            }
            else
            {
                // update the start term to the 1st not empty string
                string[] bits = start.Split(new string[] { "" + year }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in bits)
                {
                    if (s != string.Empty)
                    {
                        start = s;
                        break;
                    }
                }
            }

            // remove the quality item from the msg
            if (quality != string.Empty)
            {
                string[] bits = start.Split(new string[] { quality }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in bits)
                {
                    if (s != string.Empty)
                    {
                        start = s;
                        break;
                    }
                }
            }

            string format = FindFormat(start);
            if (format != string.Empty)
            {
                start = start.Replace(format, " ");
            }

            name = RemoveDoubleSpaces(start);


            MovieData d = new MovieData(name, quality, year, extension);
            return d;
        }

        /// <summary>Remove double spaces from a string</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static string RemoveDoubleSpaces(string input)
        {
            string output = input;
            while (output.IndexOf("  ") != -1)
            {
                // then start still contains a double space
                output = output.Replace("  ", " ");
            }
            return output.Trim();
        }
        /// <summary>
        /// Get the extension from a filename
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        static string GetExtension(string filename)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            return fi.Extension;
        }

        /// <summary>
        /// Find a quality rating for the file
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        static string FindQuality(string msg)
        {
            string[] quals = new string[] { "DVD", "TELESYNC", "TS", "CAM" };
            string lw = msg.ToUpper();
            string output = string.Empty;
            foreach (String qu in quals)
            {
                if (lw.IndexOf(qu) != -1)
                {
                    // then this quality level is the quality level of the file
                    output = qu;
                    break;
                }

            }
            switch (output)
            {
                case "TS":
                    output = "TELESYNC";
                    break;
            }
            return output;
        }

        /// <summary>
        /// Find a format in the filename
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        static string FindFormat(string msg)
        {
            string[] formats = new string[] { "DIVX" };
            string large = msg.ToUpper();

            foreach (String f in formats)
            {
                if (large.IndexOf(f) != -1)
                {
                    // then this quality level is the quality level of the file
                    return f;
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// Find a year from a string
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        static int FindYear(string msg)
        {
            Regex numbers = new Regex("[1-2]{1}[0-9]{3}");
            foreach (Match m in numbers.Matches(msg))
            {
                int target = int.Parse(m.Value);
                if (target > 1900 && target <= DateTime.Now.Year)
                {
                    // then this is a valid year
                    return target;
                }
            }
            return 0;
        }

    }
}
